package com.example.luckysystem.controller;

import com.example.luckysystem.common.errorcode.ControllerErrorCodeConstants;
import com.example.luckysystem.common.exception.ControllerException;
import com.example.luckysystem.common.pojo.CommonResult;
import com.example.luckysystem.common.util.JacksonUtil;
import com.example.luckysystem.controller.param.CreatePrizeParam;
import com.example.luckysystem.controller.param.PageParam;
import com.example.luckysystem.controller.result.FindPrizeListResult;
import com.example.luckysystem.service.PictureService;
import com.example.luckysystem.service.PrizeService;
import com.example.luckysystem.service.dto.PageListDTO;
import com.example.luckysystem.service.dto.PrizeDTO;
import jakarta.validation.Valid;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.stream.Collectors;

/**
 * @version 1.0
 * @Author shangjialu
 * @Date 2024/12/11 23:56
 * @注释
 */
@RestController
public class PrizeController {
    private static final Logger logger = LoggerFactory.getLogger(PrizeController.class);
    @Autowired
    private PictureService pictureService;

    @Autowired
    private PrizeService prizeService;

    @RequestMapping("/pic/upload")
    public String uploadPic(MultipartFile file) {
        return pictureService.savePicture(file);
    }

    //穿件奖品事宜表单的形式来创建的
    // @RequestPart注解用户于接受表单数据
    @RequestMapping("/prize/create")
    public CommonResult<Long> createPrize(
            @Valid @RequestPart("param") CreatePrizeParam param,
            @RequestPart("prizePic") MultipartFile picFile){
        logger.info("PrizeController createPrize createPrizeParam:{}",
                JacksonUtil.writeValueAsString(param));
            return CommonResult.success(prizeService.createPrize(param,picFile));
    }

    @RequestMapping("/prize/find-list")
    public CommonResult<FindPrizeListResult> findPrizeList(PageParam param) {
        //这里没有使用注解进行注解，是因为需要这里的参数直接拼接到请求中，不需要进行格式转换
        logger.info("PrizeController findPrizeList PageParam:{}", JacksonUtil.writeValueAsString(param));
        PageListDTO<PrizeDTO> pageListDTO = prizeService.findPrizeList(param);
        return CommonResult.success(convertToFindPrizeListResult(pageListDTO));
    }

    private FindPrizeListResult convertToFindPrizeListResult(PageListDTO<PrizeDTO> pageListDTO) {
        if (null == pageListDTO) {
            throw new ControllerException(ControllerErrorCodeConstants.FIND_PRIZE_LIST_ERROR);
        }
        FindPrizeListResult result = new FindPrizeListResult();
        result.setTotal(pageListDTO.getTotal());
        result.setRecords(
                pageListDTO.getRecords().stream().map(prizeDTO -> {
                            FindPrizeListResult.PrizeInfo prizeInfo = new FindPrizeListResult.PrizeInfo();
                            prizeInfo.setPrizeId(prizeDTO.getPrizeId());
                            prizeInfo.setPrizeName(prizeDTO.getName());
                            prizeInfo.setDescription(prizeDTO.getDescription());
                            prizeInfo.setImageUrl(prizeDTO.getImageUrl());
                            prizeInfo.setPrice(prizeDTO.getPrice());
                            return prizeInfo;
                }).collect(Collectors.toList())
        );
        return result;
    }
}
